//index.js
var app = getApp()
// 语言
var util = require('../../utils/util.js')
import event from '../../utils/event'

Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 8000,
    duration: 800,
    swiperCurrent: 0,
    selectCurrent: 0,
    activeCategoryId: 0,
    loadingMoreHidden: true,
    search: true,
    nonehidden: true,
    //语言 - begin
    language: '',
    //语言 - end
    searchidden: true,
    categories:[]

  },
  // 语言 
  // 设置language变量（翻译Object）
  setLanguage() {
    this.setData({
      language: wx.T.getLanguage()
    });
    //this.initCategory();
  },
  changeLanguage() {
    this.setData({
      language: wx.T.getLanguage()
    });
    this.initCategory();
  },
  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.dataset.index
    });
  },
  levelClick: function (e) {
    let item= e.currentTarget.dataset;
    // wx.navigateTo({
    //   url: "/pages/search/search?name=" + e.currentTarget.dataset.name
    // })
    wx.navigateTo({
      url: "/pages/cate-list/cate-list?id=" +item.id+"&name="+item.name
    })
  },
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  search: function(e){
    var that = this
    wx.request({
      url: app.globalData.urls + '/shop/goods/list',
      data: {
        nameLike: e.detail.value
      },
      success: function (res) {
        if (res.data.code == 0) {
          var searchs = [];
          for (var i = 0; i < res.data.data.length; i++) {
            searchs.push(res.data.data[i]);
          }
          that.setData({
            searchs: searchs,
            searchidden: false,
            nonehidden: true
          });
        }else{
          that.setData({
            searchidden: true,
            nonehidden: false
          });
        }
      }
    })
    
  },
  searchfocus: function(){
    this.setData({
      search: false,
      searchinput: true
    })
  },
  searchclose: function(){
    this.setData({
      search: true,
      searchinput: false
    })
  },
  onLoad: function () {
    wx.showLoading();
    var that = this;
    // 语言
    // 设置当前页面的language变量 - 每个页面都要有
    this.setLanguage();
    event.on("languageChanged", this, this.changeLanguage); // (2)
    // 设置当前页面的language Index - 每个页面都要有
    wx.T.setLocaleByIndex(wx.T.langIndex);
    // 语言 - 结束
    wx.getSystemInfo({
      success: function (res) {
        if (res.model.search('iPhone X') != -1) {
          that.setData({
            iphone: "iphoneTop",
            iponesc: "iphonesearch"
          });
        }
      }
    })
    
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight-res.statusBarHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height: height-100-101
        });
      }
    })
  },
  initCategory: function () {
    var that = this 
    wx.request({
      url: app.globalData.urls + '/category/list',
      header: app.getRequestHeader(),
      data:{
        bdmin_user_id:util.share_bdmin_user_id(),
      },
      success: function (res) {
        if (res.data.code == 200) {
          let getData = res.data.data;
          wx.hideLoading();
    
          that.setData({
            categories: getData,
            // activeCategoryId: 0
          });

        }

      }
    })
   
  },
  toDetailsTap: function (e){
    wx.navigateTo({
      url: "/pages/goods-detail/goods-detail?id=" + e.currentTarget.dataset.id
    })
    this.setData({
      search: true,
      searchinput: false
    })
  },
  onShow: function () {
    var that = this;
    this.initCategory();
  },

})